using System;
using System.Data;
using System.Collections;

namespace SystemFrameworks.Util
{
	/// <summary>
	/// Summary description for DataSetHelper.
	/// </summary>
	public sealed class DataSetHelper
	{
		private DataSetHelper()
		{
		}

		/// <summary>
		/// Get DataSet of distinct DataColumn from source DataSet
		/// </summary>
		/// <param name="keyColumn"></param>
		/// <param name="textColumn"></param>
		/// <returns></returns>
		public static DataSet getDistinctFromColumn( DataColumn keyColumn, DataColumn textColumn )
		{
			DataSet ds = null;

			if ( keyColumn != null && textColumn != null 
				&& keyColumn.Table.Rows.Count > 0 )
			{		
				ds = new DataSet();
				DataTable dt = new DataTable();
				ds.Tables.Add( dt );

				string keyName = keyColumn.ColumnName;
				string textName = textColumn.ColumnName;

				dt.Columns.Add( keyName, keyColumn.DataType );
				dt.Constraints.Add( "PrimaryKey", dt.Columns[ keyName ] , true );
				

				bool isDifference = keyColumn != textColumn;

				if ( isDifference )
				{
					dt.Columns.Add( textName, textColumn.DataType );
				}

				foreach( DataRow dr in keyColumn.Table.Rows )
				{
					
					DataRow drNew = dt.NewRow();
					object objKey = dr[ keyName ];
					string keyValue = Convert.ToString( objKey );
					if ( keyValue.Trim().Length > 0 )
					{
						drNew[ keyName ] = objKey;
						if ( isDifference )
						{
							drNew[ textName ] = dr[ textName ];
						}

						DataRow foundRow = dt.Rows.Find( objKey );
						if ( foundRow == null )
						{
							dt.Rows.Add( drNew );
						}
					}
				}
			}

			return ds;
		}

		/// <summary>
		/// Get DataSet of distinct DataColumn from several source DataSet
		/// </summary>
		/// <param name="keyColumn"></param>
		/// <param name="textColumn"></param>
		/// <param name="dsCollection"></param>
		/// <returns></returns>
		public static DataSet getDistinctFromColumn( string textName, string keyName, params DataSet[] dsCollection )
		{
			DataSet ds = null;
			keyName = keyName.Trim();
			textName = textName.Trim();
			bool isDifference = !keyName.Equals( textName );
		
			// Make DataSet, DataTable with key DataColumn and text DataColumn
			ds = new DataSet();
			DataTable dt = new DataTable();
			ds.Tables.Add( dt );
			dt.Columns.Add( keyName, typeof( string ) );
			dt.Constraints.Add( "PrimaryKey", dt.Columns[ keyName ] , true );
			if ( isDifference )
			{
				dt.Columns.Add( textName, typeof( string ) );
			}

			// Add distinct data to each DataColumn
			foreach( DataSet dsSource in dsCollection )
			{
				if ( dsSource == null 
					|| dsSource.Tables.Count == 0 
					|| dsSource.Tables[0].Rows.Count == 0 )
				{
					continue;
				}

				DataColumn keyColumn = dsSource.Tables[0].Columns[ keyName ];
				DataColumn textColumn = dsSource.Tables[0].Columns[ textName ];

				if ( keyColumn != null && textColumn != null 
					&& keyColumn.Table.Rows.Count > 0 )
				{

					foreach( DataRow dr in keyColumn.Table.Rows )
					{
					
						DataRow drNew = dt.NewRow();
						object objKey = dr[ keyName ];
						string keyValue = Convert.ToString( objKey );
						if ( keyValue.Trim().Length > 0 )
						{
							drNew[ keyName ] = objKey;
							if ( isDifference )
							{
								drNew[ textName ] = dr[ textName ];
							}

							DataRow foundRow = dt.Rows.Find( objKey );
							if ( foundRow == null )
							{
								dt.Rows.Add( drNew );
							}
						}
					}
				}
			}

			return ds;
		}
	}
}
